let app = getApp()

import {
    userProfile,
    getUserInfoRq
} from "../../../api/user/user.js"


Page({

    data: {
        sex: ['男', '女'],
        sexindex: 0,
        showDialog: false,
        ifName: true,
        DOMAIN_NAME: app.IMG_NAME,
        park: [],
        parkindex: 0,
        parkName: '',
        customerName: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow(e) {
        let _this = this;
        let that = this
        that.setData({
            DOMAIN_NAME: app.img_Name
        })
        that.getList()
        //
        let userId = wx.getStorageSync('userId');
        if (userId) {
            getUserInfoRq(userId).then(res => {
                console.log('getUserInfoRq', res);
                let userDetail = res.data;
                _this.setData({
                    customerName: userDetail.customerName,
                    parkName: wx.getStorageSync('parkName')
                })
            })
        } else {
            app.getlogin()
            return
        }
    },

    // 跳转园区
    jumpPark() {
        wx.navigateTo({
            url: '/pages/index/parkList/parkList',
        })
    },

    cancel() {
        this.setData({
            ifName: true
        })
    },

    change(e) {
        this.setData({
            name: e.currentTarget.dataset.name,
            ifName: false
        })
    },

    setValue(e) {
        if (this.data.name == '姓名') {
            this.setData({
                nickname: e.detail.value
            })
        } else {
            this.setData({
                post: e.detail.value
            })
        }
    },

    confirm() {
        let that = this
        if (this.data.name == '姓名') {
            app.AjaxRequest('POST', {
                'content-type': 'application/json',
                'Authorization': 'Bearer ' + app.Getopenid()
            }, '/user/profile/update', {
                username: that.data.nickname
            }, function (ret) {
                that.setData({
                    ifName: true
                })
                that.getList()
            })
        } else {
            app.AjaxRequest('POST', {
                'content-type': 'application/json',
                'Authorization': 'Bearer ' + app.Getopenid()
            }, '/user/profile/update', {
                post: that.data.post
            }, function (ret) {
                that.setData({
                    ifName: true
                })
                that.getList()
            })
        }
    },

    // 性别选择
    sexChange: function (e) {
        let that = this
        that.setData({
            sexindex: e.detail.value
        })
        app.AjaxRequest('POST', {
            'content-type': 'application/json',
            'Authorization': 'Bearer ' + app.Getopenid()
        }, '/user/profile/update', {
            gender: JSON.parse(e.detail.value)
        }, function (ret) {
            that.getList()
        })
    },
    getList() {
        let that = this
        userProfile(wx.getStorageSync('userId')).then(res => {
            that.setData({
                memberInfo: res.data,
                sexindex: res.data.gender
            })
        })
    },
    chooseimage: function () {
        var that = this
        let imgUrl = that.data.imgUrl
        wx.chooseImage({
            count: 1, // 默认9 
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
            success: function (res) {
                console.log(res.tempFilePaths[0])
                wx.navigateTo({
                    url: '../cropper/cropper?img=' + res.tempFilePaths[0],
                })
            }
        })
    },
    navCropper() {
        this.chooseimage()
    },
    exitaccount() {
        wx.showModal({
            title: '提示',
            content: '确定退出账号吗',
            success(res) {
                if (res.confirm) {
                    wx.clearStorageSync()
                    wx.reLaunch({
                        url: '/pages/login/login',
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    }
})